package com.hmall.common.interceptor;

import com.hmall.common.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class GetUserIdentityInterceptor implements HandlerInterceptor {

    //private UserUtil userUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String authorization = request.getHeader("authorization");
        log.info("解析userId:{}",authorization);

        if (!StringUtils.isEmpty(authorization)) {
            Long userId = Long.parseLong(authorization);
            if (userId == 2L){
                UserUtil.setParameter(userId);
                System.out.println(UserUtil.getParameter());
                return true;
            }
        }
        response.setStatus(401);
        return false;
    }
}
